package com.neusoft.neumooc.dao;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.TemplateVO;

@Repository
public class TemplateDAOImpl extends BaseDAO implements TemplateDAO {

    private static final String SQL_GET_ALL_BUILTIN_TEMPLATES = 
        "SELECT tplFile, tplName, tplType, custom, tplDesc, content " +
        "FROM cw_templates WHERE custom = '0'";
        
    private static final String SQL_GET_ALL_CUSTOM_TEMPLATES = 
        "SELECT tplFile, tplName, tplType, custom, tplDesc, content " +
        "FROM cw_templates WHERE custom = '1'";
        
    private static final String SQL_GET_TEMPLATE_BY_FILE = 
        "SELECT tplFile, tplName, tplType, custom, tplDesc, content " +
        "FROM cw_templates WHERE tplFile = :tplFile";
        
    private static final String SQL_UPDATE_TEMPLATE = 
        "UPDATE cw_templates SET tplName = :tplName, tplType = :tplType, " +
        "tplDesc = :tplDesc, content = :content WHERE tplFile = :tplFile";
        
    private static final String SQL_DELETE_TEMPLATE = 
        "DELETE FROM cw_templates WHERE tplFile = :tplFile";
        
    private static final String SQL_ADD_TEMPLATE = 
        "INSERT INTO cw_templates(tplFile, tplName, tplType, custom, tplDesc, content) " +
        "VALUES (:tplFile, :tplName, :tplType, :custom, :tplDesc, :content)";

    private static final String SQL_GET_TEMPLATES_BY_TYPE = 
        "SELECT tplFile, tplName, tplType, custom, tplDesc, content " +
        "FROM cw_templates WHERE tplType = :tplType";

    @Override
    public List<TemplateVO> getAllBuiltinTemplates() throws DAOException {
        try {
            return this.queryForBeanList(SQL_GET_ALL_BUILTIN_TEMPLATES, TemplateVO.class);
        } catch (Exception e) {
            log.error("查询内置模板列表失败", e);
            throw new DAOException("查询内置模板列表失败", e);
        }
    }
    
    @Override
    public List<TemplateVO> getAllCustomTemplates() throws DAOException {
        try {
            return this.queryForBeanList(SQL_GET_ALL_CUSTOM_TEMPLATES, TemplateVO.class);
        } catch (Exception e) {
            log.error("查询自定义模板列表失败", e);
            throw new DAOException("查询自定义模板列表失败", e);
        }
    }

    @Override
    public TemplateVO getTemplateByFile(String tplFile) throws DAOException {
        try {
            return this.queryForBeanByBeanMap(SQL_GET_TEMPLATE_BY_FILE, TemplateVO.class, 
                    new java.util.HashMap<String, Object>() {{
                        put("tplFile", tplFile);
                    }});
        } catch (Exception e) {
            log.error("查询模板失败", e);
            throw new DAOException("查询模板失败", e);
        }
    }

    @Override
    public boolean updateTemplate(TemplateVO template) throws DAOException {
        try {
            return this.update(SQL_UPDATE_TEMPLATE, template) > 0;
        } catch (Exception e) {
            log.error("更新模板失败", e);
            throw new DAOException("更新模板失败", e);
        }
    }

    @Override
    public boolean deleteTemplate(String tplFile) throws DAOException {
        try {
            return this.update(SQL_DELETE_TEMPLATE, 
                    new java.util.HashMap<String, Object>() {{
                        put("tplFile", tplFile);
                    }}) > 0;
        } catch (Exception e) {
            log.error("删除模板失败", e);
            throw new DAOException("删除模板失败", e);
        }
    }
    
    @Override
    public boolean addTemplate(TemplateVO template) throws DAOException {
        try {
            return this.update(SQL_ADD_TEMPLATE, template) > 0;
        } catch (Exception e) {
            log.error("添加模板失败", e);
            throw new DAOException("添加模板失败", e);
        }
    }

    @Override
    public List<TemplateVO> getTemplatesByType(String tplType) throws DAOException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("tplType", tplType);
            log.info("***************" + tplType);
            return this.queryForBeanListByBeanMap(SQL_GET_TEMPLATES_BY_TYPE, TemplateVO.class, params);
        } catch (Exception e) {
            log.error("查询模板列表失败", e);
            throw new DAOException("查询模板列表失败", e);
        }
    }
} 